namespace :js do
  desc "Compiles all js libs into the Sunray runtime"
  task :build => [:environment] do
    require "sprockets"
    require 'fileutils'
    require 'yaml'    
    sunray_config = YAML::load( File.open( 'public/javascripts/application/config/loadpaths.yml' ) )
    includes      = YAML::load( File.open( 'public/javascripts/application/config/libmix.yml' ) )

    config = [
      "public/javascripts/application/config/config.js"
    ]
    
    apis = includes["base libraries"]
    
    ui = [
      "public/javascripts/application/dependencies.js"
    ]
    
    runtime = config + apis + ui
    secretary = Sprockets::Secretary.new(
      :asset_root   => "public",
      :load_path    => sunray_config["load paths"],
      :source_files => runtime
    )
    concatenation = secretary.concatenation
    concatenation.save_to(sunray_config["output location"]["runtime"])
    
    sh %{java -jar public/javascripts/application/tools/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar public/javascripts/runtime.js -o public/javascripts/runtime.js}


    #sh %{ java org.mozilla.javascript.tools.shell.Main public/javascripts/application/tools/jslint.js public/javascripts/runtime.js }
  end
end

